package sk.tuke.constraintbuilder.views.components;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.List;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.ui.dialogs.ListSelectionDialog;
import org.eclipse.ui.model.BaseWorkbenchContentProvider;
import org.eclipse.ui.model.WorkbenchLabelProvider;

import sk.tuke.constraintbuilder.analyzer.RulesetManager;
import sk.tuke.constraintbuilder.model.Constraint;
import sk.tuke.constraintbuilder.model.Model;
import sk.tuke.constraintbuilder.model.Ruleset;

public class RulesetManagerView extends Dialog {
	
	//Selected project
	private IProject selectedProject;

	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public RulesetManagerView(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = (GridLayout) container.getLayout();
		gridLayout.numColumns = 3;
		
		Label lblProjects = new Label(container, SWT.NONE);
		lblProjects.setText("Projects:");
		
		Label lblNewLabel = new Label(container, SWT.NONE);
		lblNewLabel.setText("Rulesets:");
		new Label(container, SWT.NONE);
		
		final ListViewer projectViewer = new ListViewer(container, SWT.BORDER | SWT.V_SCROLL);
		List list = projectViewer.getList();
		GridData gd_list = new GridData(SWT.FILL, SWT.FILL, false, true, 1, 2);
		gd_list.widthHint = 188;
		list.setLayoutData(gd_list);
		final ListViewer rulesetViewer = new ListViewer(container, SWT.BORDER | SWT.V_SCROLL);
		List list_1 = rulesetViewer.getList();
		list_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		
		rulesetViewer.setContentProvider(ArrayContentProvider.getInstance());
		rulesetViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				Ruleset ruleset = (Ruleset) element;
				return String.valueOf(ruleset.getName());
			};
		});
		

		projectViewer.setContentProvider(ArrayContentProvider.getInstance());
		projectViewer.setLabelProvider(new LabelProvider() {
			@Override
			public String getText(Object element) {
				IProject project = (IProject) element;
				return String.valueOf(project.getName());
			};
		});
		projectViewer.setInput(ResourcesPlugin.getWorkspace().getRoot().getProjects());
		projectViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				ISelection selection = projectViewer.getSelection();
        		if (selection != null && selection instanceof IStructuredSelection) {
        			Object obj = ((IStructuredSelection) selection).getFirstElement();
        			if (obj instanceof IProject) {
        				selectedProject = (IProject) obj;
        				rulesetViewer.setInput(RulesetManager.getInstance().getRulesets(selectedProject.getName()));
        				rulesetViewer.refresh();
        			}
        		}
			}
		});
		if(projectViewer.getList().getItemCount()>0){
			projectViewer.getList().select(0);
		}

		
		Composite composite_1 = new Composite(container, SWT.NONE);
		composite_1.setLayout(new RowLayout(SWT.VERTICAL));
		
		Button addButton = new Button(composite_1, SWT.NONE);
		addButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				LabelProvider labelProvider =  new LabelProvider() {
					@Override
					public String getText(Object element) {
						Ruleset ruleset = (Ruleset) element;
						return String.valueOf(ruleset.getName());
					};
				};
	    		final ListSelectionDialog dlg = new ListSelectionDialog(getShell(), Model.getModel().getRulesets(), ArrayContentProvider.getInstance(), labelProvider, "Select Rulesets:");
	    		dlg.setTitle("Ruleset Selection");
	    		dlg.open();
	    		for (Object ruleset : dlg.getResult()) {
					if(ruleset instanceof Ruleset){
						RulesetManager.getInstance().applyRulesetOnProject(selectedProject, (Ruleset) ruleset);
						rulesetViewer.setInput(RulesetManager.getInstance().getRulesets(selectedProject.getName()));
						rulesetViewer.refresh();
					}
				}
			}
		});
		addButton.setText("Add Ruleset..");
		
		Button removeButton = new Button(composite_1, SWT.NONE);
		removeButton.setText("Remove Ruleset");
		removeButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				ISelection rulesetSelection = rulesetViewer.getSelection();
        		if (rulesetSelection != null && rulesetSelection instanceof IStructuredSelection) {
        			Object obj = ((IStructuredSelection) rulesetSelection).getFirstElement();
        			if (obj instanceof Ruleset && selectedProject!=null) {
        				RulesetManager.getInstance().removeRulesetFromProject(selectedProject, (Ruleset) obj);
        				rulesetViewer.setInput(RulesetManager.getInstance().getRulesets(selectedProject.getName()));
        				rulesetViewer.refresh();
        			}
        		}
			}
		});
		
		Composite composite = new Composite(container, SWT.BORDER);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 2, 1));

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(710, 467);
	}
}
